<?php
include 'common.php';
include 'mysqli.php';

//实例化db类
$db = new Db(['host'=>'localhost','user'=>'root','pass'=>'root','name'=>'capture']);
$db2 = new Db(['host'=>'localhost','user'=>'root','pass'=>'root','name'=>'youchai']);
$dir = './QQ/';
//当前目录下文件名及大小

$currentFile = read_file($dir);
//print_r($currentFile);exit;

//数据库中保存的文件名及大小
$allFile = select_file($db);
//print_r($allFile);exit;
if (count($currentFile) == count($allFile)) {
    foreach ($allFile as $k=>$v) {
        if ($v['name'] == $currentFile[$k]['filename']) {
            //数据库中保存的文件大小比当前文件小
            if ($v['size'] < $currentFile[$k]['filesize']) {
                record_file($currentFile[$k]['filename'],$currentFile[$k]['filesize'],$db);
                $text = new_text($dir.$currentFile[$k]['filename'],$v['size']);
                $items = array_filter(explode("\r\n\r\n",$text));
                $tmp_arr = [];
                //去除时间，然后合并相同用户发送的内容
                foreach ($items as $item) {
                    $tmp_arr[] = mb_substr($item,20);
                }
                $new_items = array_unique($tmp_arr);
                foreach ($new_items as $item) {
                    if ($key = filter_info($item)) {
                        record_business($key,$currentFile[$k]['filename'],$item,$db2);
                    }
                }
            }
        }
    }
} else {
    //判断是否是新文件
    foreach ($currentFile as $v) {
        $res = $db->getOne('text',"name='$v[filename]'",'name,size');
        if (!$res) {
            $db->insertOne('text','name,size',"'$v[filename]','$v[filesize]'");
        }
    }
}
?>
<html>
<meta http-equiv="refresh" content="10;url=#" />
</html>
